package org.cttic.visualization.visualization.util.sixUtil;

import org.apache.kafka.common.metrics.stats.Count;
import org.cttic.visualization.visualization.common.module.provinceData;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author shkstart
 * @create 2020-05-27 16:22
 */
@Component
public class interactiveQueryUtil {
    public static final String access_indecess="access_info_202005_test";
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    //路网调用北斗次数
    public long queryCount_access(String provinceId,String startTime,String endTime){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(startTime).lte(endTime));
        boolQueryBuilder.must(QueryBuilders.prefixQuery("vehiclePlate", provinceData.province.get(Integer.parseInt(provinceId))));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        searchSourceBuilder.query(boolQueryBuilder);
//        searchSourceBuilder.sort("tansTime");
//        searchSourceBuilder.size(0);

        // 执行搜索,向ES发起http请求
        String StartMonth = startTime.substring(5, 7);
        String index1 ="access_info_2020"+StartMonth+"_test";
        System.out.println("access_info_2020"+StartMonth+"_test");
        String endMonth = endTime.substring(5, 7);
        String index2 ="access_info_2020"+endMonth+"_test";
        System.out.println("access_info_2020"+endMonth+"_test");
        CountRequest Request;
        if(index1.equals(index2)){
            Request = new CountRequest(index1);
        }else {
            Request = new CountRequest(index1,index2);
        }
        Request.types("_doc");

        Request.source(searchSourceBuilder);
        CountResponse rp = null;
        try {

            rp = restHighLevelClient.count(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);

        return rp.getCount();
    }

    public long queryCount_info(String provinceId,String startTime,String endTime){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(startTime).lte(endTime));
        boolQueryBuilder.must(QueryBuilders.prefixQuery("vehicleNo", provinceData.province.get(Integer.parseInt(provinceId))));
//        searchSourceBuilder.aggregation(AggregationBuilders.cardinality("qc_vehicleNo").field("vehiclePlate"));
        searchSourceBuilder.query(boolQueryBuilder);
//        searchSourceBuilder.sort("tansTime");
//        searchSourceBuilder.size(0);

        // 执行搜索,向ES发起http请求
        String StartMonth = startTime.substring(5, 7);
        String index1 ="trip_info_2020"+StartMonth;
        String index2 ="trip_incomplete_2020"+StartMonth;
        System.out.println(index1+"\n"+index2);
        String endMonth = endTime.substring(5, 7);
        String index3 ="trip_info_2020"+endMonth;
        String index4 ="trip_incomplete_2020"+StartMonth;
        System.out.println(index3+"\n"+index4);
        CountRequest Request;
        if(index1.equals(index2)){
            Request = new CountRequest(index1,index2);
        }else {
            Request = new CountRequest(index1,index2,index3,index4);
        }

        Request.types("_doc");

        Request.source(searchSourceBuilder);
        CountResponse rp = null;
        try {

            rp = restHighLevelClient.count(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);

        return rp.getCount();
    }
    public long queryCountMultiple_access(String provinceId,String startTime,String endTime){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(startTime).lte(endTime));
        //单省
        if(provinceId!=null&&!provinceId.contains("|")){
            System.out.println("****************查询单省****************");
            boolQueryBuilder.must(QueryBuilders.prefixQuery("vehiclePlate", provinceData.province.get(Integer.parseInt(provinceId))));
            //多省
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            if(split.length>0){
                System.out.println("****************查询多省****************");
                for (String s : split) {
                    boolQueryBuilder.should(QueryBuilders.prefixQuery("vehiclePlate",
                            provinceData.province.get(Integer.parseInt(s))));
                }
            }
        }
        searchSourceBuilder.query(boolQueryBuilder);
        // 执行搜索,向ES发起http请求
        String StartMonth = startTime.substring(5, 7);
        String index1 ="access_info_2020"+StartMonth;
        System.out.println("access_info_2020"+StartMonth);
        String endMonth = endTime.substring(5, 7);
        String index2 ="access_info_2020"+endMonth;
        System.out.println("access_info_2020"+endMonth);
        CountRequest Request;
        if(index1.equals(index2)){
            Request = new CountRequest(index1);
        }else {
            Request = new CountRequest(index1,index2);
        }

        Request.types("_doc");

        Request.source(searchSourceBuilder);
        CountResponse rp = null;
        try {

            rp = restHighLevelClient.count(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp.getCount());

        return rp.getCount();
    }
    public long queryCountMultiple_info(String provinceId,String startTime,String endTime){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime").gte(startTime).lte(endTime));
        //单省
        if(provinceId!=null&&!provinceId.contains("|")){
            boolQueryBuilder.must(QueryBuilders.prefixQuery("vehicleNo", provinceData.province.get(Integer.parseInt(provinceId))));
         //多省查询
        }else if(provinceId!=null&&provinceId.contains("|")){
            String[] split = provinceId.split("\\|");
            if(split.length>0){
                for (String s : split) {
//                    boolQueryBuilder.must(QueryBuilders.matchPhraseQuery("vehicleNo","A"));
                    boolQueryBuilder.should(QueryBuilders.prefixQuery("vehicleNo",
                            provinceData.province.get(Integer.parseInt(s))));
                    System.out.println(provinceData.province.get(Integer.parseInt(s)));
                }
            }
        }
        System.out.println(boolQueryBuilder.toString());
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.size(0);
        // 执行搜索,向ES发起http请求
        String StartMonth = startTime.substring(5, 7);
        String index1 ="trip_info_2020"+StartMonth;
        String index2 ="trip_incomplete_2020"+StartMonth;
        System.out.println(index1+"\n"+index2);
        String endMonth = endTime.substring(5, 7);
        String index3 ="trip_info_2020"+endMonth;
        String index4 ="trip_incomplete_2020"+StartMonth;
        System.out.println(index3+"\n"+index4);
        SearchRequest Request;
        if(index1.equals(index2)){
            Request = new SearchRequest(index1,index2);
        }else {
            Request = new SearchRequest(index1,index2,index3,index4);
        }

        Request.types("_doc");

        Request.source(searchSourceBuilder);
        SearchResponse rp = null;
        try {

            rp = restHighLevelClient.search(Request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("rp:"+rp);

        return rp.getHits().getTotalHits();
    }
}
